package com.ruoyi.framework.config;

import com.ruoyi.common.utils.ip.IpUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class RequestInfoInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse response, Object handler) throws Exception {
        // 在请求被处理之前执行
        System.out.println("请求被拦截: " + httpServletRequest.getRequestURI());
        String ipAddr = IpUtils.getIpAddr(httpServletRequest);
        System.out.println(ipAddr);
        return true; // 返回true则继续执行后续操作，返回false则中断请求
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 在请求被处理之后，视图渲染之前执行
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 在整个请求结束之后执行
    }
}
